package com.mijie.homi.search;




import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.json.JSONArray;

import org.apache.log4j.Logger;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;


import com.mijie.core.IBatisDao;
import com.mijie.homi.search.api.TopicSearchApi;
import com.mijie.homi.search.api.UserSearchApi;
import com.mijie.homi.search.dao.mood.MoodDao;
import com.mijie.homi.search.service.index.ClrRecently;
import com.mijie.homi.search.service.index.MoodUserIndexService;
import com.mijie.homi.search.util.god;
import com.mijie.homi.search.util.ibtsT;
import com.mijie.homi.search.util.listUtil;
import com.mijie.homi.search.util.pagingController;
import com.mijie.homi.search.util.securyInt;
import com.mijie.homi.search.util.tryX;
import com.mijie.query.sincinController;

import static com.mijie.homi.search.service.index.MoodUserIndexService.*;

/**
 * 【匹配中】 3、匹配的时候，只在问题库的所有问题中进行检索 。
 * 
 * @author Administrator
 * 
 */

// @Resource(type = SearchResultProser.class) //(value =
// "SearchResultProserCmpt")
@Component @SuppressWarnings("all")
@Scope("prototype")
public class SearchResultProser extends MoodUserIndexService {

	@Autowired
	@Qualifier("moodUserIndexService")
	MoodUserIndexService matchService;
	public int offset;
	public int limit;
	public int userId;
	public Map<String, Map> msgs_mp;
	public List<String> moodIds_li;
	public List<Map> msg_wait_hid_byMktDelPrblm;

	public List<Map> del_Msg_ByHeminLen;

	public Map<String, Map> moods_mp;

	// private List<String> del_sincin_markDelete_set;
	// private List<Map> msg_del_by_gender;
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			ibtsT.iniProxool();
			ApplicationContext ac = UserSearchApi.getApplicationContext("");
			SearchResultProser c = (SearchResultProser) ac
					.getBean("searchResultProser");
			SearchResultProser c2 = (SearchResultProser) ac
					.getBean("searchResultProser");
			// List<Map> li = .selectList("getRecently100", nowId);

			System.out.println(c);
			c.start();
			System.out.println("f");

			System.out.println(c);
			System.out.println(c2);
System.out.println("-------------=================");
			TopicSearchApi c3 = (TopicSearchApi) ac.getBean("topicSearchApi");
			TopicSearchApi c4 = (TopicSearchApi) ac.getBean("topicSearchApi");
//			System.out.println(c3.SearchResultProserThis);
//			System.out.println(c4.SearchResultProserThis);
			System.out.println(c3.createSearchResultProserTest());
			System.out.println(c4.createSearchResultProserTest());

//			List<String> ids = c.sqlSession.selectList("getNonReviewTopic",
//					"23904,23903,23906");
//			String eventStrformatx = JSONArray.fromObject(ids).toString(2);
//			logger.info(eventStrformatx);
			
			Map<String,Object> parameter = new HashMap<String,Object>();
			parameter.put("tid", 123);
			parameter.put("uid", 670970);
			int mychoiceID= securyInt.getInt( c3.sqlSession.selectOne("getOptionIdByUid", parameter),0);
		for(int i=0;i<3;i++)
		{
			final int topicID=5;
		}
			System.out.println(mychoiceID);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			System.exit(0);
		}

	}

	private void start() {
		System.out.println("ooo");

	}

	/**
	 * 
	 【匹配后】 4、匿名的问题可以显示
	 * 
	 * 5、排除自己，自己发的问题排除掉
	 * 
	 * 6、以问题为计量，同一个用户发的问题不需要排除
	 * 
	 * @param r2
	 * @return
	 */
	
	public List process(List<Map> r2) {
		// Nbo order by pipeidu,createAt ,ar only pipeidu
//		try {
//			matchService.orderByCreateTime(r2);
//
//		} catch (Exception e) {
//			matchService.logger.info(god.getTrace(e));
//		}
		// nbp order by active user
		// r2 = order_by_active_user(r2);
		// logger.info("  return all:" + r2.size());

		// ///
		List<Map> li3 = r2;
		// tbn hide not public
		// li3 = exceptHide(li3, moodIds_except, prblmIds_except);
		// //
		// Nbo del prblm if sincin jenmye
		// try {
		// if (sincinController.isJenmyerSincin(mood.getMood()))
		// li3 = del_prblm(prblmIds_li, li3, moods_mp);
		// } catch (Exception e) {
		// logger.info(god.getTrace(e));
		// }

		// Nbp del sincin markDelete
		// li3 = del_sincin_markDelete(del_sincin_markDelete_set, li3,
		// moods_mp);

		// nbt del bpipei d sincinN prblm by hemin len
		// li3 = del_byHeminLen(del_Msg_ByHeminLen, li3);

		// o11
		// li3 = delMsg_by_gender(msg_del_by_gender, li3);

		// o11 duli user
		// de duli user
		// userID_deduli_set = new HashSet<String>();
		// msg_wait_del_byDuliUser = new ArrayList<Object>();
		// for (Map m : li3) {
		//
		// String uid_s = String.valueOf(m.get("userId"));
		//
		// if (userID_deduli_set.contains(uid_s))
		// msg_wait_del_byDuliUser.add(m);
		// userID_deduli_set.add(uid_s);
		// }

		// li3 = delMsg_by_byDuliUser(msg_wait_del_byDuliUser, li3);

		// o12
		// li3 = delMsg_by_byAnonymosPrblm(msg_wait_hid_byAnonymosPrblm, li3);
		li3 = delMsg_by_byMktDelPrblm(msg_wait_hid_byMktDelPrblm, li3);
		// li3 = delMsg_block_sincin(block_sincin, li3);
		// o16
		li3 = delMsg_dedw(li3);

		// o17 except uid
		li3 = except_self(li3, userId, msgs_mp, moodIds_li);

		// o1c except review (shenha)
		li3 = except_review(li3, msgs_mp, moodIds_li);

		// List<Map> li3 =ftc. except_uidByMids(sincinIds_set, new
		// ArrayList<String>(), li3);

		// tbn paging.
		logBefro(li3);
		
		//o1f 
		if(offset<6)
			offset=0;
		List<Map> li4 = pagingController.newPage(offset, limit, li3);
		
		
		logiAft(li4);
		 
		// li4 = countDistans(userId, li4);
		 li4 =setLastIdxf(li4,this.idxf+10);
		return li4;

	}

	private void logiAft(List<Map> li4) {
		logger_o1f.debug("---------------------------paginged:");
		for(Map m:li4)
		{
			logger_o1f.debug(m.get("title") );
		}
	}

	private void logBefro(List<Map> li3) {
		List ori_li_debug=getori_li_debug(li3);
		for(Map m:li3)
		{
			logger_o1f.debug(m.get("title") );
		}
	}
	private List getori_li_debug(List<Map> li3) {
		// TODO Auto-generated method stub
		return null;
	}
	public static Logger logger_o1f = Logger.getLogger("matchPrblmPaging");
	private List<Map> setLastIdxf(final List<Map> li4, final int i) {
		List r = (List) tryX.tryStart(new tryX() {
			@Override
			public Object item(Object t) throws Exception {
				this.defaultReturnValue = li4; 
				for (Map m : li4) {
					m.put("idxf", String.valueOf(i));
				}
				return li4;
			}
 
		});

		return r;
	}

	@Autowired
	private SqlSessionTemplate sqlSession;
	@Autowired
	MoodDao MoodDaoInstc;
	@Autowired
	@Qualifier("moodDao")
	IBatisDao IBatisDaoInstc;
	private int idxf;

	/**
	 * 排除没有审核的问题
	 * 
	 * @param li3
	 * @param msgsMp
	 * @param moodIdsLi
	 * @return
	 */
	private List<Map> except_review(List<Map> li3, Map<String, Map> msgsMp,
			List<String> moodIdsLi) {
		try {
			List<Object> ids = sqlSession.selectList("getNonReviewTopic",
					listUtil.toString(moodIdsLi));
			for (Object id : ids) {

				Map m = msgsMp.get("pr" + id.toString());
				li3.remove(m);
			}
		} catch (Exception e) {
			logger.warn(god.getTrace(e));
		}
		return li3;

	}

	public Object invokeMethod(Object owner, String methodName, Object[] args)
			throws Exception {

		Class ownerClass = owner.getClass();

		Class[] argsClass = new Class[args.length];

		for (int i = 0, j = args.length; i < j; i++) {
			argsClass[i] = args[i].getClass();
		}

		Method method = ownerClass.getMethod(methodName, argsClass);

		return method.invoke(owner, args);
	}

	public SearchResultProser setParam(int offset2, int limit2, int userId2,
			Map<String, Map> msgsMp, List<String> moodIdsLi,
			List<String> moodIdsLi2, List msgWaitHidByMktDelPrblm,
			List delMsgByHeminLen, Map<String, Map> moodsMp) {
		this.offset = offset2;
		this.limit = limit2;
		this.userId = userId2;
		this.msgs_mp = msgsMp;
		this.moodIds_li = moodIdsLi;
		this.msg_wait_hid_byMktDelPrblm = msgWaitHidByMktDelPrblm;
		this.del_Msg_ByHeminLen = delMsgByHeminLen;
		this.moods_mp = moodsMp;
		return this;

	}

	public void setIdxf(String idxf) {
		this.idxf=securyInt.getInt(idxf, 0) ;
		
	}

}
